package com.easyar.customizdblibrary;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;

/**
 * Created by Sigthp-P-Department on 2018/4/11.
 */

public class CustomisDBDao<T> implements ICustomisDBDao<T>{

    private SQLiteDatabase mSqLiteDatabase=null;
    private Class<T> mClazz=null;
    private String tableName;
    private QueryDB mQueryDB;

    @Override
    public void init(SQLiteDatabase mSqLiteDatabase, Class<T> clazz) {
        this.mSqLiteDatabase=mSqLiteDatabase;
        this.mClazz=clazz;

        StringBuffer sb=new StringBuffer();
        //创建表 sql语句
        //"create table if not exists Person (id integer primary key autoincrement,name text, age integer,flag boolean)"
        tableName = clazz.getSimpleName();
        sb.append("create table if not exists ").append(tableName).append(" (id integer primary key autoincrement, ");
        Field[] fields = mClazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            String type = field.getType().getSimpleName();
            sb.append(name).append(" ").append(DBUtile.getColumnType(type)).append(", ");
        }
        //把stringbuffer的最后一个", ”替换成")"
        sb.replace(sb.length() - 2, sb.length(), ")");
        mSqLiteDatabase.execSQL(sb.toString());
    }

    @Override
    public long insert(T t) {
        ContentValues contentValues = null;
        try {
            contentValues = getContentValues(t);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return mSqLiteDatabase.insert(tableName, null, contentValues);
    }

    @Override
    public void insert(List<T> datas) {
        mSqLiteDatabase.beginTransaction();
        for (T t : datas) {
            insert(t);
        }
        mSqLiteDatabase.setTransactionSuccessful();
        mSqLiteDatabase.endTransaction();
    }

    @Override
    public QueryDB<T> queryDB() {
        if (mQueryDB == null){
            mQueryDB = new QueryDB<T>(mSqLiteDatabase,mClazz);
        }
        return mQueryDB;
    }

    /**
     * 原生删除方式
     */
    @Override
    public int delete(String whereClause, String[] whereArgs) {
        return mSqLiteDatabase.delete(mClazz.getSimpleName(), whereClause, whereArgs);
    }

    /**
     * 原生更新方式
     */
    @Override
    public int update(T t, String whereClause, String... whereArgs) {
        ContentValues values = null;
        try {
            values = getContentValues(t);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return mSqLiteDatabase.update(mClazz.getSimpleName(),
                values, whereClause, whereArgs);
    }

    public ContentValues getContentValues(T t) throws IllegalAccessException {
        ContentValues contentValues=new ContentValues();
        Class<?> tClass = t.getClass();
        Field[] fields = tClass.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            Object obj = field.get(t);
            String type = field.getType().getSimpleName();
            DBUtile.setContentValue(contentValues,obj,name,type);
        }
        return contentValues;
    }

}
